package com.itboye.sunsun.login.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.itboye.homesensen.alert.ByAlert;
import com.itboye.sunsunhome.R;
import com.itboye.sunsun.application.App;
import com.itboye.sunsun.beans.PersonDataBean;
import com.itboye.sunsun.constants.SPContants;
import com.itboye.sunsun.controller.LoginController;
import com.itboye.sunsun.controller.LoginedState;
import com.itboye.sunsun.network.HttpRequest;
import com.itboye.sunsun.person.ui.PersonSettingActivity;
import com.itboye.sunsun.support.BaseActivity;
import com.itboye.sunsun.utils.DebugLog;
import com.itboye.sunsun.utils.SPUtils;
import com.itboye.sunsun.volley.CodeErrorException;
import com.itboye.sunsun.volley.MyJsonRequest;
import com.itboye.sunsun.volley.XErrorListener;
import com.itboye.sunsun.volley.XRequestListener;

public class RegistActivity extends BaseActivity implements OnClickListener {
	private EditText phoneedit;
	private EditText yzmedit;
	private EditText passwordedit;
	private EditText inviteedit;
	private String username;
	private String yzm;
	private String password;
	private String invite;
	private Button yzmbutton;
	private Button registbutton;
	private ImageView back;
	private TimeCount time;
	protected String code;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_regist);
		// initData();
		time = new TimeCount(60000, 1000, yzmbutton);// 构造CountDownTimer对象
		setStatusBarColor(R.color.home_blue);
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.yzmbutton:
			username = phoneedit.getText().toString();
			if (!username.equals("")) {
				getyzm();
				time.start();
			} else {
				ByAlert.alert("请输入手机号");
			}
			break;
		case R.id.registbutton:
			userregist();
			break;
		case R.id.back:
			this.finish();
			break;
		default:
			break;
		}
	}

	private void getyzm() {
		MyJsonRequest<String> request = new MyJsonRequest.Builder<String>()
				.apiVer("100").typeKey("BY_Message_send_sms")
				.param("mobile", phoneedit.getText().toString())
				.param("code_type", "1")
				.param("app_type","2")
				.requestListener(new XRequestListener<String>() {

					@Override
					public void onResponse(String response) {
						// // TODO Auto-generated method stub
						// DebugLog.v("result", response.toString());
						// yzmedit.setText(response);
						code = response;
					}
				}).errorListener(new XErrorListener() {

					@Override
					public void onErrorResponse(Exception exception, int code,
							String msg) {
						if (exception instanceof CodeErrorException) {
							Toast.makeText(App.ctx, msg, 0).show();
						} else {
							Toast.makeText(App.ctx, exception.toString(), 0)
									.show();
						}

					}
				}).build();
		HttpRequest.getDefaultRequestQueue().add(request);
	}

	private void userregist() {
		if (yzmedit.getText().toString().trim() == null) {
			ByAlert.alert("请输入验证码");
			return;
		}
		MyJsonRequest<String> request = new MyJsonRequest.Builder<String>()
				.apiVer("100").typeKey("BY_User_register").param("from", "0")
				.param("code_type", "1").param("reg_type", "3")
				.param("password", "" + passwordedit.getText().toString())
				.param("code", yzmedit.getText().toString())
				// .param("invite_id",""+inviteedit.getText().toString())
				.param("username", "" + phoneedit.getText().toString())
				.requestListener(new XRequestListener<String>() {

					@Override
					public void onResponse(String response) {
						// TODO Auto-generated method stub
						DebugLog.v("result", response.toString());
						// Toast.makeText(App.ctx, "注册成功", 0).show();
						LoginController.setLoginState(new LoginedState());
						SPUtils.put(App.ctx, null, SPContants.USER_ID, response);
						SPUtils.put(App.ctx, null, SPContants.USERNAME,
								phoneedit.getText().toString());
						SPUtils.put(App.ctx, null, SPContants.MOBILE, phoneedit
								.getText().toString());
						// SPUtils.put(App.ctx, null, SPContants.PASSWORD,
						// passwordedit.getText().toString());
						SPUtils.put(App.ctx, null, SPContants.IS_LOGINED, true);

						login();
						RegistActivity.this.finish();

						// 发送广播通知LoginActivity结束
						Intent intent1 = new Intent(LoginActivity.LOGINEXIT);
						sendBroadcast(intent1);
						LoginController.setLoginState(new LoginedState());

					}
				}).errorListener(new XErrorListener() {

					@Override
					public void onErrorResponse(Exception exception, int code,
							String msg) {
						if (exception instanceof CodeErrorException) {
							Toast.makeText(App.ctx, msg, 0).show();
						} else {
							Toast.makeText(App.ctx, exception.toString(), 0)
									.show();
						}

					}
				}).build();
		HttpRequest.getDefaultRequestQueue().add(request);
	}

	// public static class TimeCount extends CountDownTimer {
	// public TimeCount(long millisInFuture, long countDownInterval) {
	// super(millisInFuture, countDownInterval);// 参数依次为总时长,和计时的时间间隔
	// }
	//
	// @Override
	// public void onFinish() {// 计时完毕时触发
	// yzmbutton.setText("重新验证");
	// yzmbutton.setClickable(true);
	// }
	//
	// @Override
	// public void onTick(long millisUntilFinished) {// 计时过程显示
	// yzmbutton.setClickable(false);
	// yzmbutton.setText(millisUntilFinished / 1000 + "秒");
	// }
	// }

	public void login() {
		MyJsonRequest<PersonDataBean> request = new MyJsonRequest.Builder<PersonDataBean>()
				.apiVer("100").typeKey("BY_User_login")
				.param("username", phoneedit.getText().toString())
				.param("password", passwordedit.getText().toString())
				.requestListener(new XRequestListener<PersonDataBean>() {

					@Override
					public void onResponse(PersonDataBean response) {
						// TODO Auto-generated method stub
						DebugLog.v("result", response.toString());

						SPUtils.put(App.ctx, null, SPContants.WX_Openid,
								response.getWxopenid());
						SPUtils.put(App.ctx, null, SPContants.USERNAME,
								response.getUsername());
						SPUtils.put(App.ctx, null, SPContants.MOBILE,
								response.getMobile());
						SPUtils.put(App.ctx, null, SPContants.IS_LOGINED, true);
						SPUtils.put(App.ctx, null, SPContants.EMAIL,
								response.getEmail());
						SPUtils.put(App.ctx, null, SPContants.NICK,
								response.getNickname());
						SPUtils.put(App.ctx, null, SPContants.PASSWORD,
								response.getPassword());
						SPUtils.put(App.ctx, null, SPContants.USER_ID,
								response.getUid());
						SPUtils.put(App.ctx, null, SPContants.AVATAR_URL,
								response.getHead());

						Intent intent1 = new Intent(
								PersonSettingActivity.EXITCHANGE);
						sendBroadcast(intent1);

					}
				}).errorListener(new XErrorListener() {

					@Override
					public void onErrorResponse(Exception exception, int code,
							String msg) {
						if (exception instanceof CodeErrorException) {
							Toast.makeText(App.ctx, "网络异常", 0).show();
						} else {
							Toast.makeText(App.ctx, "网络异常", 0).show();
						}

					}
				}).build();
		HttpRequest.getDefaultRequestQueue().add(request);
	}
}
